<?php

class LoginController extends Controller {

    public function actionIndex() {
        $this->layout = '//layouts/login';

        if (isset($_GET['sbm'])) {
            $model = new login_form;

            // if it is ajax validation request
            if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }

            // collect user input data
            if (isset($_POST['login_form'])) {
                $model->attributes = $_POST['login_form'];

                // validate user input and redirect to the previous page if valid
                if ($model->validate() && $model->login())
                    $this->redirect('home/dashboard');
            }
            // display the login form
            //$this->render('index', array('model' => $model));
            $this->renderPartial('login', array('model' => $model));
        } else {
            $this->redirect('/');
        }
    }

}